package com.ruoyi.biz.letter.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.tokenizer.TokenizerEngine;
import cn.hutool.extra.tokenizer.TokenizerUtil;
import cn.hutool.extra.tokenizer.Word;
import com.google.common.collect.Lists;
import com.ruoyi.common.core.domain.entity.BizCategory;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.service.ISysDeptService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import javax.annotation.PostConstruct;
import java.io.StringReader;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Fei.Cong
 * @date 2024-07-23 16:43
 */
@Slf4j
@Component
public class SysDeptUtil {

    @SneakyThrows
    public static Long getDeptId(String letterNum, String address) {
        String district = "";
        List<SysDept> deptList = SpringUtils.getBean(RedisCache.class).getCacheList("deptList");
        if (CollUtil.isEmpty(deptList)) {
            deptList = SpringUtils.getBean(ISysDeptService.class).listAllDept(new SysDept());
            SpringUtils.getBean(RedisCache.class).deleteObject("deptList");
            SpringUtils.getBean(RedisCache.class).setCacheList("deptList", deptList);
        }
        Long deptId = 100L;
        StringReader sr = new StringReader(address);
        IKSegmenter ik = new IKSegmenter(sr, true);
        Lexeme lex = null;
        while ((lex = ik.next()) != null) {

            if (StrUtil.containsAny(lex.getLexemeText(), "市", "区")) {
                district = lex.getLexemeText();
                String finalDistrict = district;
                Optional<SysDept> sysDept = deptList.stream().filter(v -> Objects.equals(v.getDeptName(), finalDistrict)).findAny();
                deptId = sysDept.get().getDeptId();
                break;
            }

        }
        return deptId;
    }

    @SneakyThrows
    public static List<Long> getChildList(Long deptId) {
        if (Objects.isNull(deptId)) {
            return Lists.newArrayList();
        }
        SysDept dept = new SysDept();
        dept.setDeptId(deptId);
        List<SysDept> deptList = SpringUtils.getBean(RedisCache.class).getCacheList(CacheUtil.getDeptChildKey(deptId.toString()));
        if (CollUtil.isEmpty(deptList)) {
            deptList = SpringUtils.getBean(ISysDeptService.class).selectLocalLevelAndChildrenDeptById(dept);
            SpringUtils.getBean(RedisCache.class).deleteObject(CacheUtil.getDeptChildKey(deptId.toString()));
            SpringUtils.getBean(RedisCache.class).setCacheList(CacheUtil.getDeptChildKey(deptId.toString()), deptList);
        }
        return deptList.stream().map(SysDept::getDeptId).collect(Collectors.toList());
    }

    public static SysDept getDept(Long deptId) {
        return SpringUtils.getBean(RedisCache.class).getCacheObject(CacheUtil.getDeptKey(deptId.toString()));
    }

    public static String getDetailDeptName(Long deptId) {
        SysDept dept = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheUtil.getDeptKey(deptId.toString()));
        SysDept parentDept = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheUtil.getDeptKey(dept.getParentId().toString()));
        return new StringBuffer(parentDept.getDeptName()).append("-").append(dept.getDeptName()).toString();
    }

    public static SysDept getUserDept(String username) {
        if(StrUtil.isNotBlank(username)){
            SysUser user = SpringUtils.getBean(RedisCache.class).getCacheObject(CacheUtil.getUserKey(username));
            return getDept(user.getDeptId());
        }
        return new SysDept();
    }

}
